package com.mairuide._frame.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 树形节点模型 mh
 */
public class Node {
	private Integer id;
	private String nodeId;// 树的节点Id，区别于数据库中保存的数据Id。
	private String pid;
	private String text; // 节点名称
	private String icon;
	private String[] tags = new String[] { "1" };
	private Boolean selectable;
	private Integer nodeLeve;
	private List<Node> nodes; // 子节点，可以用递归的方法读取
	private State state = new State();
	public class State{
		//是否默认展开节点
		private Boolean expanded = true;
		private Boolean selecttable = true;
		
		public Boolean getExpanded() {
			return expanded;
		}
		public void setExpanded(Boolean expanded) {
			this.expanded = expanded;
		}
		public Boolean getSelecttable() {
			return selecttable;
		}
		public void setSelecttable(Boolean selecttable) {
			this.selecttable = selecttable;
		}
		
	}
	public Node() {
		this.nodes = new ArrayList<Node>();
	}

	public Node(String nodeId, String pid) {
		this.nodeId = nodeId;
		this.pid = pid;
		this.nodes = new ArrayList<Node>();
	}

	/**
	 * 生成一个节点
	 * 
	 * @param nodeId
	 * @param pId
	 * @param text
	 * @param icon
	 * @param href
	 */
	public Node(String nodeId, String pid, String text, String icon) {
		super();
		this.nodeId = nodeId;
		this.pid = pid;
		this.text = text;
		this.icon = icon;
		this.nodes = new ArrayList<Node>();
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public List<Node> getNodes() {
		return nodes;
	}

	public void setNodes(List<Node> nodes) {
		this.nodes = nodes;
	}

	public Boolean getSelectable() {
		return selectable;
	}

	public void setSelectable(Boolean selectable) {
		this.selectable = selectable;
	}

	public String[] getTags() {
		return tags;
	}

	public void setTags(String[] tags) {
		this.tags = tags;
	}

	public Integer getNodeLeve() {
		return nodeLeve;
	}

	public void setNodeLeve(Integer nodeLeve) {
		this.nodeLeve = nodeLeve;
	}
	
	public State getState() {
		return state;
	}

	public void setState(State state) {
		this.state = state;
	}

	
	/**
	 * 生成一颗多叉树，根节点为root
	 * @param Nodes  生成多叉树的节点集合
	 * @return
	 */
	public Node createTree(List<Node> Nodes) {
		if (Nodes == null || Nodes.size() < 0)
			return null;
		Node root = new Node("root", "0");// 根节点自定义，但是要和pid对应好
		// 将所有节点添加到多叉树中
		for (Node node : Nodes) {
			if (node.getPid().equals("0")) {// 根节点自定义，但是要和pid对应好
				// 向根添加一个节点
				root.getNodes().add(node);
			} else {
				addChild(root, node);
			}
		}
		return root;
	}

	/**
	 * 向指定多叉树节点添加子节点
	 * 
	 * @param Node 多叉树节点
	 * @param child 节点
	 */
	public void addChild(Node Node, Node child) {
		for (Node item : Node.getNodes()) {
			if (item.getNodeId().equals(child.getPid())) {
				// 判断是否是第三级节点
				if (child.getNodeLeve() == 3) {
					child.setNodes(null);
				}
				// 找到对应的父亲
				item.getNodes().add(child);
				break;
			} else {
				if (item.getNodes() != null && item.getNodes().size() > 0) {
					addChild(item, child);
				}
			}
		}
	}

	/**
	 * 遍历多叉树
	 * 
	 * @param Node
	 *            多叉树节点
	 * @return
	 */
	public String iteratorTree(Node Node) {
		StringBuilder buffer = new StringBuilder();
		buffer.append("\n");
		if (Node != null) {
			for (Node index : Node.getNodes()) {
				buffer.append(index.getNodeId() + ",");
				if (index.getNodes() != null && index.getNodes().size() > 0) {
					buffer.append(iteratorTree(index));
				}
			}
		}
		buffer.append("\n");
		return buffer.toString();
	}
}